// import mNetTool from "./NetTool.js"

loginWx = function(mNetTool, fn) {
console.log('获取用户登录状态 ！'  )
	var that = this;
	uni.login({
		success: function(res) {
			var code = res.code;
			if (!code) {
				console.log('获取用户登录状态失败！' + res.errMsg)
				return;
			}
			uni.setStorageSync('code', code);
			mNetTool.getAjax("capi/xcx/login.do", {
				code: code,
			}, function(data) {
				console.log(data.userInfo);
				//本地缓存用户信息
				uni.setStorageSync('userInfo', data.userInfo)
				getApp().globalData.userInfo = data.userInfo;
				if (data.userInfo.mobile != undefined && data.userInfo.mobile) {
					uni.setStorageSync('phoneNumber', data.userInfo.mobile);
				};
				uni.setStorageSync("user_id", data.user_id);
				uni.setStorageSync("token", data.token);
				uni.setStorageSync("userType", data.userInfo.type);
	
				if(data.userInfo.wx_synchronization_flag&&data.userInfo.wx_synchronization_flag==1){
					uni.setStorageSync('isLogin',true)
				}
				let userType = uni.getStorageSync('userType')
				typeof fn == "function" && fn(data);
			})
		}
	});

}

var main = {

	init(mNetTool, fn) {
		console.log('j进入了init')
	 
		let userInfo = uni.getStorageSync('userInfo')
		if (!uni.getStorageSync('token') && !userInfo) {
			 
			// #ifdef MP-WEIXIN
			loginWx(mNetTool, fn)
			// #endif
		} else {
			if(userInfo.wx_synchronization_flag&&userInfo.wx_synchronization_flag==1){
				uni.setStorageSync('isLogin',true)
			}
			typeof fn == "function" && fn();
		}
	}
}
module.exports = main;
